//
//  CFindPublishedNewsFlashViewController.m
//  Btpin
//
//  Created by Vic on 2019/12/17.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "CFindPublishedNewsFlashViewController.h"
#import "PublishedNewsFlashCell.h"
#import "GeneralWebViewViewController.h"
#import "PMineInviteFriendViewController.h"

@interface CFindPublishedNewsFlashViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSMutableArray *itemDataArray;
    
    UITableView *cTableView;
    
    NSInteger page;
}
@end

@implementation CFindPublishedNewsFlashViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    itemDataArray = [NSMutableArray arrayWithCapacity:0];
    page = 1;
    
    [self loadData];
    [self initUI];
}
- (void)loadData {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:[NSString stringWithFormat:@"%ld", (long)page] forKey:@"page"];
    [dic setObject:PageSize forKey:@"number"];
    
    [BaseNetManager requestWithMethod:POST path:Published_NewsFlash_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            if ([data isKindOfClass:[NSArray class]]) {
                for (NSDictionary *dic in data) {
                    PublishedNewsFlashModel *model = [[PublishedNewsFlashModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self->itemDataArray addObject:model];
                }
                self.noDataView.hidden = YES;
                [self->cTableView.mj_footer endRefreshing];
                [self->cTableView reloadData];
            }
            if (self->itemDataArray.count) {
            } else {
                self.noDataView.hidden = NO;
                [self.noDataView vcNilStatusType:VCNilStatusTypeOfCommonNoData superView:self.view];
            }
            SVPHUDDismiss;
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
        [self.noDataView vcNilStatusType:VCNilStatusTypeOfNoNet superView:self.view];
    }];
}
- (void)initUI {
    self.navigationItem.title = @"已发快讯";
    self.view.backgroundColor = THEMECOLOR_OF_WHITE;
    
    cTableView = [UIToolsControl initTableViewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-SIMEmojKeyBoardkBottomNOSafeArea_Height-HeightOfStatusBarAndNavigationBar) target:self style:UITableViewStylePlain];
    cTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    cTableView.backgroundColor = COLOR_OF_CLEAR;
    [self.view addSubview:cTableView];
    
    __weak __typeof(self)weakSelf = self;
    cTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self->page++;
        [self->cTableView.mj_footer beginRefreshing];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf loadData];
        });
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return itemDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PublishedNewsFlashCell *cell = [tableView dequeueReusableCellWithIdentifier:isCellID];
    if (!cell) {
        cell = [[PublishedNewsFlashCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:isCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    PublishedNewsFlashModel *model = itemDataArray[indexPath.row];
    [cell reloadDataForModel:model];
    
    return cell;
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    PublishedNewsFlashModel *model = itemDataArray[indexPath.row];
    
    if ([model.audit isEqualToString:@"0"]) {
        CustomView *alertView = [[CustomView alloc] initWithTitle:@"提示" message:model.audit_mark prompt:nil sureBtn:@"确定" cancleBtn:nil size:170];
        alertView.resultIndex = ^(NSInteger index) { //回调---处理一系列动作
            if (index == 2) {
                
            }
        };
        [alertView showXLAlertView];
    } else if ([model.audit isEqualToString:@"1"]) {
//        GeneralWebViewViewController *VC = [[GeneralWebViewViewController alloc] init];
//        VC.showTabBar = NO;
//        VC.url = model.url;
//        VC.showType = GeneralWebViewTypeOfNewsFlash;
//        [self.navigationController pushViewController:VC animated:YES];
        
        PMineInviteFriendViewController *VC = [[PMineInviteFriendViewController alloc] init];
        VC.url = model.url;
        VC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:VC animated:YES];
    } else if ([model.audit isEqualToString:@"2"]) {
        CustomView *alertView = [[CustomView alloc] initWithTitle:@"提示" message:model.audit_mark prompt:nil sureBtn:@"确定" cancleBtn:nil size:170];
        alertView.resultIndex = ^(NSInteger index) { //回调---处理一系列动作
            if (index == 2) {
                
            }
        };
        [alertView showXLAlertView];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    PublishedNewsFlashModel *model = itemDataArray[indexPath.row];
    return model.cellHeight;
}

@end
